class Solution {
public:
    std::vector<std::string> split(const std::string& s, char delim) {
        std::vector<std::string> elems;
        std::stringstream ss(s);
        std::string item;
        while (getline(ss, item, delim)) {
            elems.push_back(item);
        }
        return elems;
    }
    bool wordPattern(string pattern, string s) {
        vector<pair<char, string>> a;
        vector<string> b = split(s, ' ');
            
        if (b.size() != pattern.size())
            return false;  // 增加大小匹配检查

        for (int i = 0; i < pattern.size(); ++i) {
            bool found = false;
            for (int j = 0; j < a.size(); ++j) {
                if (a[j].first == pattern[i]) {
                    found = true;  // 找到匹配项
                    if (a[j].second != b[i])
                        return false;
                }
            }

            if (!found) {  // 如果没有匹配项，则添加新项
                for (int k = 0; k < a.size(); ++k) {
                    if (a[k].second == b[i])
                        return false;  // 如果b[i]已经被映射过，则返回false
                }
                a.push_back({pattern[i], b[i]});
            }
        }
        return true;
    }
};
